package com.du.springmvc.controller;

import com.du.springmvc.exception.UsernameNotFoundException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author : DuJiabao
 * @Project : spring-mvc
 * @Time : 2021/4/7 19:22
 * @Desc :
 */
@Controller
public class ErrorController {
  @RequestMapping("/div")
  public String div(Integer num) {
    int res = 10 / num;
    return "success";
  }

  @ExceptionHandler(ArithmeticException.class)
  public ModelAndView arithmeticExceptionHandler(Exception e) {
    System.out.println("发生了 " + e);
    ModelAndView mv = new ModelAndView("error");
    mv.addObject("exception", e);
    return mv;
  }

  @RequestMapping("/usernfTest")
  public String usernfTest(String username) {
    if (!"admin".equals(username)) throw new UsernameNotFoundException();
    return "success";
  }

  @RequestMapping(value = "/postRequest", method = RequestMethod.POST)
  public String postRequest() {
    System.out.println("post 请求");
    return "success";
  }
}
